﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
using System.Data;


namespace DesktopDFS
{
    public struct MeetingData
    {
        public String startdate { set; get; }
        public String starttime { set; get; }
        public String category { set; get; }
        public String subject { set; get; }
    }

    /// <summary>
    /// Interaction logic for map.xaml
    /// </summary>
    public partial class map : Window
    {
        public map()
        {
            InitializeComponent();

            // Create connection 
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "Server=127.0.0.1;"
                + "Database=dfs_superschema;"
                + "Uid=root;"
                + "Password=;";
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            try
            {
                //dataGrid1.Columns[0].CellStyle = System.Type.GetType("System.Date");
                //dataGrid1.Columns["ColumnName"].DataType = System.Type.GetType("System.Date");

                MySqlCommand command = new MySqlCommand("Select startdate, starttime, category, objectlevel from meeting", conn);
                MySqlDataReader reader = command.ExecuteReader();

                ((DataGridTextColumn)dataGrid1.Columns[0]).Binding = new Binding("startdate");
                ((DataGridTextColumn)dataGrid1.Columns[1]).Binding = new Binding("starttime");
                ((DataGridTextColumn)dataGrid1.Columns[2]).Binding = new Binding("category");
                ((DataGridTextColumn)dataGrid1.Columns[3]).Binding = new Binding("subject");
                
                while (reader.Read())
                {
                    dataGrid1.Items.Add(new MeetingData() { startdate = reader.GetString(0).Substring(0, 10), starttime = reader.GetString(1), category = reader.GetString(2), subject = reader.GetString(3) });
                }
                reader.Close();
                conn.Close();
            }
            finally
            {
                
            }

        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {
            expander1.Header = "Less";
        }

        private void expander1_Collapsed(object sender, RoutedEventArgs e)
        {
            expander1.Header = "More";
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        
        private void dataGrid1_Sorting(object sender, DataGridSortingEventArgs e)
        {
            //override DataGridSortingEventHandler(){
                MySqlConnection conn = new MySqlConnection();
                conn.ConnectionString = "Server=127.0.0.1;"
                    + "Database=dfs_superschema;"
                    + "Uid=root;"
                    + "Password=;";
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                if (e.Column == dataGrid1.Columns[0])
                {
                    MySqlCommand command = new MySqlCommand("Select startdate, starttime, category, objectlevel from meeting order by startdate desc", conn);
                    MySqlDataReader reader = command.ExecuteReader();

                    ((DataGridTextColumn)dataGrid1.Columns[0]).Binding = new Binding("startdate");
                    ((DataGridTextColumn)dataGrid1.Columns[1]).Binding = new Binding("starttime");
                    ((DataGridTextColumn)dataGrid1.Columns[2]).Binding = new Binding("category");
                    ((DataGridTextColumn)dataGrid1.Columns[3]).Binding = new Binding("subject");

                    while (reader.Read())
                    {
                        dataGrid1.Items.Add(new MeetingData() { startdate = reader.GetString(0).Substring(0, 10), starttime = reader.GetString(1), category = reader.GetString(2), subject = reader.GetString(3) });
                    }
                    reader.Close();
                    conn.Close();
                }
                e.Handled = true;
            //}
        }
        //private void dataGrid1_Sorting(object sender, DataGridSortingEventArgs e)
        //{ }
    }
}
